package com.springx.examples.showcase.controller;


import com.google.common.collect.Maps;
import com.springx.examples.showcase.common.domain.Message;
import com.springx.examples.showcase.entity.Area;
import com.springx.examples.showcase.enums.BaseEnum;
import com.springx.examples.showcase.service.AreaService;
import com.springx.examples.showcase.vo.tree.AreaVo;
import com.springx.examples.showcase.vo.tree.TreeTableVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.Assert;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.*;

@Controller
@RequestMapping("/area")
public class AreaController extends BaseController {
    @Autowired
    private AreaService areaService;

    @RequestMapping("/index")
    public String index(Model model,String name,String status) {
        model.addAttribute(CRUMBS_ATTRIBUTE, buildCrumbsMap(null, "系统管理", "地区管理"));
        model.addAttribute("statusItems", EnumSet.allOf(BaseEnum.IS_ENABLED.class));
        return "area/index";
    }



    @RequestMapping("/list")
    public @ResponseBody TreeTableVo list1(Long id,Model model,String name,String status) {
        Map<String, Object> params= Maps.newHashMap();
        params.put("parentId",null==id?-1:id);
        if(StringUtils.isNotBlank(status)){
            params.put("status",status);
        }
        if(StringUtils.isNotBlank(name)){
            params.put("name", name);
        }
        PageRequest pageRequest = new PageRequest(1, 10);
        Page<AreaVo> pageResult =areaService.findPage(params,pageRequest);
        TreeTableVo treeTableVo=new TreeTableVo();
        treeTableVo.setRows(pageResult.getContent());
        return  treeTableVo;
    }
    





    @RequestMapping("/insert")
    public
    @ResponseBody
    Message insert(@Valid Area area) {
        area.setCreateDate(new Date());
       if(area.getParentId()==-1){
           area.setLevel(0l);
       }
        else {
           Area  area1=areaService.select(area.getParentId());
           area.setLevel(area1.getLevel()+1);
       }
        areaService.insert(area);
        Message message = new Message(Message.StatusType.SUCCESS, "保存成功");
        return message;
    }

    /**
     * 跳转到edit页面
     *
     * @return
     */
    @RequestMapping("/edit_none")
    public String edit(Long id, Model model, String type) {
         Area  area;
        if ("edit".equals(type)) {
            area=areaService.select(id);
            model.addAttribute("area", area);
            if(area.getParentId()!=-1){
                Area  ar=areaService.select(area.getParentId());
                model.addAttribute("ar",ar);
            }
        }
        if ("add".equals(type)) {
            area=areaService.select(id);
            model.addAttribute("ar", area);
        }
        return "area/edit";
    }

    /**
     * update
     *
     * @return
     */
    @RequestMapping("/update")
    public
    @ResponseBody
    Message update(@Valid Area area) {

        Assert.notNull(area.getId(), "参数错误");
        Area updateArea = areaService.select(area.getId());
        if (updateArea != null) {
            updateArea.setName(area.getName());
            updateArea.setCode(area.getCode());
            updateArea.setDescription(area.getDescription());
            updateArea.setStatus(area.getStatus());
            updateArea.setCreateDate(new Date());
            areaService.updateByPrimaryKeySelective(updateArea);
        }
        Message message = new Message(Message.StatusType.SUCCESS, "保存成功");

        return message;
    }


    @RequestMapping("/delete")
    public
    @ResponseBody
    Message delete(@RequestParam(value = "ids[]") Long[] ids) {
        areaService.delete(ids);
        Message message = new Message(Message.StatusType.SUCCESS, "删除成功");

        return message;
    }







    public  static Map<String, Object> or(String order ){
        Map<String, Object> searchParam = Maps.newHashMap();
        if (order==null){
            order="asc";
        }
        if("desc".equals(order)){
            searchParam.put("desc",order);
        }
        if("asc".equals(order)){
            searchParam.put("asc",order);
        }
        return  searchParam;
    }
}
